package com.ruoqing.oss.service.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.model.PutObjectResult;
import com.ruoqing.core.constant.OssConstants;
import com.ruoqing.oss.factory.OssClientFactory;
import com.ruoqing.oss.service.OssUpLoadService;

import java.io.File;
import java.net.URL;
import java.util.Date;

/**
	* @author YaoXian
	* @title: OssUpLoadServiceImpl
	* @projectName ruoxi-blog
	* @description: 若兮微服务 - 文件服务 - 文件上传接口实现类
	* @date 2020-12-29 23:52
	*/
public class OssUpLoadServiceImpl implements OssUpLoadService {
		
		public String upLoadImg(File file,String username) {
				String imgName  = username + ".jpg";
				Date expiration = new Date(System.currentTimeMillis() + OssConstants.DATE_TIME);
				OSS ossClient = OssClientFactory.createOssClient();
				PutObjectResult putObjectResult = ossClient.putObject(OssConstants.BUCKET_NAME_IMG, imgName, file);
				URL url = ossClient.generatePresignedUrl(OssConstants.BUCKET_NAME_IMG, imgName, expiration);
				return url.toString();
		}
}
